﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BluePumpkin.Library.AppCode.Context;

namespace BluePumpkin.Library
{
    class AccountManager
    {

        public void Add(Users user)
        {
            try
            {
                using (BdbContext DBContext = new BdbContext())
                {

                    Users _user = new Users
                    {
                        DOB = user.DOB,
                        LoginId = user.LoginId,
                        Name = user.Name,
                        Password = user.Password,
                        UserTypes = new UserTypes()
                        {
                            UserID = user.Id,
                            Name = user.UserTypes.Name,
                            Status = user.UserTypes.Status

                        }
                    };
                    DBContext.Users.Add(_user);
                    DBContext.SaveChanges();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            

        }
        public List<Users>  Getdata()
        {
            List<Users> data =  new List<Users>();
            try
            {

                using (BdbContext context = new BdbContext())
                {
                    var obj = (from f in context.Users select f).ToList();

                    foreach (var item in obj)
                    {
                        data.Add(item);
                    }
                 
                   
                }

                return data;
            }
            catch (Exception)
            {
                return data;

                throw;
            }
        }
        public void Update(Users user)
        {
            try
            {
                using (BdbContext DBContext = new BdbContext())
                {
                    var _user = DBContext.Users.Single(x => x.Id == user.Id);

                    _user.DOB = user.DOB;
                    _user.LoginId = user.LoginId;
                    _user.Name = user.Name;
                    _user.Password = user.Password;
                    _user.UserTypes = new UserTypes()
                    {
                        UserID = user.Id,
                        Name = user.UserTypes.Name,
                        Status = user.UserTypes.Status

                    };


                    DBContext.SaveChanges();
                }

            }
            catch (Exception)
            {
                
                throw;
            }
           
        }


        public void Delete(Users user)
        {
            try
            {
                using (BdbContext DBContext = new BdbContext())
                {
                    var _user = DBContext.Users.Single(x => x.Id == user.Id);


                    _user.UserTypes = new UserTypes()
                    {
                        UserID = user.Id,

                        Status = user.UserTypes.Status

                    };


                    DBContext.SaveChanges();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            

        }
    }
}
